import asyncio
import datetime
from time import sleep

import aiohttp  # 使用 aiohttp 替换 requests
from DrissionPage import ChromiumPage, ChromiumOptions
from MysqlUtils import MysqlUtils

URL = "http://localhost:8081"

# 定义一个Semaphore来控制并发数
concurrent_limit = 10
semaphore = asyncio.Semaphore(concurrent_limit)


# 时间戳
def convert_timestamp_to_datetime(timestamp):
    return datetime.datetime.fromtimestamp(int(timestamp))

async def getSearchUser():
    pass

async def getReplys(video_id, comment_id, user_id):
    print("来了", video_id)
    mu = MysqlUtils()
    offset = 0  # 已经获取的
    limit = 20  # 获取的数据量
    params = {
        "id": video_id,
        'comment_id': comment_id,
        "offset": offset,
        "limit": limit
    }
    i = 0
    while True:
        try:
            i += 1
            async with aiohttp.ClientSession() as session:
                async with session.get(URL + "/douyin/replys", params=params) as res:
                    response = await res.json()
                    tow_comments = response["data"]["comments"]
                    if not tow_comments:
                        break
                    for tow_comment in tow_comments:
                        signature=""
                
                        tow_data = {
                            "name": tow_comment['user']['nickname'],
                            "cid": tow_comment['cid'],
                            "text": tow_comment['text'],
                            "sec_uid": tow_comment['user']['sec_uid'],
                            "signature": signature,
                            "create_time": convert_timestamp_to_datetime(tow_comment['create_time']),
                        }
                        print("tow_data-----", tow_data["text"])
                        if tow_data["text"] != "":
                            await asyncio.to_thread(mu.cur.execute,
                                                    "INSERT INTO pinglun_list (nickname, cid, text, sec_uid, signature, create_time,u_id) VALUES (%s, %s,%s, %s, %s, %s, %s)",
                                                    (tow_data['name'], tow_data['cid'], tow_data['text'],
                                                     tow_data['sec_uid'],
                                                     tow_data['signature'], tow_data['create_time'], user_id))
                            await asyncio.to_thread(mu.con.commit)
            offset += len(tow_comments)
            params["offset"] = offset
        except aiohttp.ClientError as e:
            print(f"HTTP error occurred: {e}")
            break
        except Exception as e:
            print(f"An error occurred in getReplys: {e}")
            break


# async def get

async def getComments(video_id, user_id):
    async with semaphore:
        print("当前视频连接", video_id)
        mu = MysqlUtils()
        offset = 0  # 已经获取的
        limit = 20  # 获取的数据量
        params = {
            "id": video_id,
            "offset": offset,
            "limit": limit
        }
        i = 0
        while True:
            i += 1
            async with aiohttp.ClientSession() as session:
                async with session.get(URL + "/douyin/comments", params=params) as res:
                    response = await res.json()
                    print(response)
                    one_comments = response["data"]["comments"]
                    if not one_comments:
                        break
                    for one_comment in one_comments:
                        signature=""
                        one_data = {
                            "name": one_comment['user']['nickname'],
                            "cid": one_comment['cid'],
                            "text": one_comment['text'],
                            "sec_uid": one_comment['user']['sec_uid'],
                            "signature": signature,
                            "create_time": convert_timestamp_to_datetime(one_comment['create_time']),
                        }
                        # print("one_data=============", one_data["text"])
                        if one_data["text"] != "":
                            await asyncio.to_thread(mu.cur.execute,
                                                    "INSERT INTO pinglun_list (nickname, cid, text, sec_uid, signature, create_time,u_id) VALUES (%s, %s, %s,%s, %s, %s, %s)",
                                                    (one_data['name'], one_data['cid'], one_data['text'],
                                                     one_data['sec_uid'], one_data['signature'],
                                                     one_data['create_time'], user_id))
                            await asyncio.to_thread(mu.con.commit)
                            await getReplys(video_id, one_comment['cid'], user_id)
            offset += len(one_comments)
            params["offset"] = offset


def addDouYin():
    params = {
        "id": "che",
        "cookie": 'your_cookie_here'
    }
    res = requests.post(URL + "/douyin/add_account", json=params)
    print(res.text)


async def main():
    mu = MysqlUtils()

    co = ChromiumOptions()

    # 如果是无界面系统，添加无头模式参数
    # co.set_argument('--headless=new')

    # 如果是Linux系统，尝试添加'--no-sandbox'启动参数
    co.set_argument('--no-sandbox')
    page = ChromiumPage(co)
    mu.cur.execute("select * from pinglun_menu where is_search!=1")
    page.listen.start("/aweme/v1/web/aweme/post/")
    # return 1
    processed_videos = set()  # 用于存储已经处理过的视频ID

    tasks = []
    for row in mu.cur.fetchall():
        print(row)
        page.get(row[1].replace(" ",""))
        sleep(0.2)
        sm = page.eles('xpath://*[@id="douyin-right-container"]/div[2]/div/div/div/div[3]/p[1]')
        # 判断是否是隐私账号
        if len(sm) != 0 or sm != []:
            continue
        try:
            res = page.listen.wait(timeout=10)
            #
            videos = res.response.body["aweme_list"]
            for video in videos:
                video_id = video['aweme_id']
                if video_id not in processed_videos:
                    print(video_id)
                    tasks.append(getComments(video_id, row[3]))
                    processed_videos.add(video_id)
            mu.cur.execute("update pinglun_menu set is_search=1 where url=%s", row[1])
            mu.con.commit()
        except Exception as e:
            print(e)
            
    await asyncio.gather(*tasks)
    page.quit()


if __name__ == "__main__":
    asyncio.run(main())
